package com.hrm.dao;

import com.hrm.entity.SalaryEntity;

import java.util.Map;
import java.util.Set;

/**
 * Create by zekdot on 20-3-4.
 */
public class SalaryDao extends BaseDao<SalaryEntity> {
    /**
     * 查找某雇员某月的薪水
     * @param id 员工id
     * @param year 年份
     * @param month 月份
     * @return
     * @throws Exception
     */
    public SalaryEntity get(String id,String year,String month) throws Exception {
        String sql = "SELECT * FROM salary WHERE e_id=? AND year=? AND month=?";
        Object[] params = {id,year,month};
        return queryOne(sql,params);
    }

    /**
     * 更新某员工某月薪水
     * @param id 员工id
     * @param year 年份
     * @param month 月份
     */
    public void update(String id, String year, String month, Map<String,Object> paramMap) throws Exception {
//        String sql = "UPDATE salary SET "
//        String tableName = getTableName();
        // 构造SQL语句
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("UPDATE ").append("salary").append(" SET ");
        // 获取Map的键集合
        Set<String> set = paramMap.keySet();

        for(String key:set){
            sqlBuilder.append(getSqlFieldName(key)).append("=").append("?,");
        }
        sqlBuilder.deleteCharAt(sqlBuilder.length()-1);
        sqlBuilder.append(" WHERE e_id=? AND year=? AND month=?");

        Object params[] = new Object[set.size()+3];
        int index = 0;
        for(String key:set){
            params[index++] = paramMap.get(key);
        }
        params[index++] = id;
        params[index++] = year;
        params[index++] = month;
        execute(sqlBuilder.toString(),params);
    }
    public static void main(String args[]){
        SalaryDao salaryDao = new SalaryDao();
        SalaryEntity salaryEntity = new SalaryEntity();
        salaryEntity.setEId("zekdot");
        salaryEntity.setBase(10000);
        salaryEntity.setBonus(1000);
        salaryEntity.setComLevel("A");
        salaryEntity.setDeduct(0);
        try {
            salaryDao.save(salaryEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
